package com.ostrich.note.ui.note.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ostrich.note.R;
import com.ostrich.note.databinding.ItemNoteBinding;
import com.ostrich.note.ui.note.entity.NoteEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;


public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {
    private ArrayList<NoteEntity> mList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;


    public ArrayList<NoteEntity> getList() {
        return mList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
    public void clearData(){
        mList.clear();

    }

    public void addData(NoteEntity note){
        mList.add(note);
        notifyItemInserted(mList.size()-1);
    }

    public void updateData(String title,String content,int position){
        NoteEntity note = mList.get(position);
        note.setTitle(title);
        note.setContent(content);
        notifyItemChanged(position);
    }


    public void deleteData(int position){
        mList.remove(position);

        notifyItemRemoved(position);
    }

    //数据做刷新
    public void setData(List<NoteEntity> list){
        if(list == null){
            return;
        }
        if(list.size() == 0){
            mList.clear();
            notifyDataSetChanged();
        }else{
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }
    //根据下标返回对应的数据
    public NoteEntity getNoteByPosition(int position){
        return mList.get(position);
    }


    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNoteBinding itemNoteBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_note, parent, false);
        return new NoteViewHolder(itemNoteBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.mItemNoteBinding.setNoteEntity(mList.get(holder.getAdapterPosition()));
        //item的点击
        holder.mItemNoteBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(v,holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class NoteViewHolder extends RecyclerView.ViewHolder{
        private ItemNoteBinding mItemNoteBinding;
        public NoteViewHolder(ItemNoteBinding itemNoteBinding) {
            super(itemNoteBinding.getRoot());
            this.mItemNoteBinding = itemNoteBinding;


        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }


}
